package com.kczd.jinlan.view.shouye.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kczd.jinlan.R;
import com.kczd.jinlan.adapter.recycleviewadapter.RecycleViewPingjiaAdapter;
import com.kczd.jinlan.model.Evaluate;
import com.kczd.jinlan.myview.pulltoreflush.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品评价模块
 * zhangy
 * Created by Administrator on 2017/5/19.
 */

public class PingjiaFragment extends Fragment implements PullLoadMoreRecyclerView.PullLoadMoreListener{

    private View view;
    private int mCount = 1;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    // private int mCount = 1;
    private RecycleViewPingjiaAdapter mRecyclerViewAdapter;
    private RecyclerView mRecyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.pingjia_fragment,null);

        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView)view. findViewById(R.id.pullLoadMoreRecyclerView);
        //获取mRecyclerView对象
        mRecyclerView = mPullLoadMoreRecyclerView.getRecyclerView();
        //代码设置scrollbar无效？未解决！
        mRecyclerView.setVerticalScrollBarEnabled(true);
        //设置下拉刷新是否可见
        //mPullLoadMoreRecyclerView.setRefreshing(true);
        //设置是否可以下拉刷新
        //mPullLoadMoreRecyclerView.setPullRefreshEnable(true);
        //设置是否可以上拉刷新
        //mPullLoadMoreRecyclerView.setPushRefreshEnable(false);
        //显示下拉刷新
        mPullLoadMoreRecyclerView.setRefreshing(true);
        //设置上拉刷新文字
        mPullLoadMoreRecyclerView.setFooterViewText("loading");
        //设置上拉刷新文字颜色
        //mPullLoadMoreRecyclerView.setFooterViewTextColor(R.color.white);
        //设置加载更多背景色
        //mPullLoadMoreRecyclerView.setFooterViewBackgroundColor(R.color.colorBackground);
        mPullLoadMoreRecyclerView.setLinearLayout();

        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        mRecyclerViewAdapter = new RecycleViewPingjiaAdapter(getActivity());
        mPullLoadMoreRecyclerView.setAdapter(mRecyclerViewAdapter);
        getData();
        return view;
    }

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRecyclerViewAdapter.addAllData(setList());
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    private List<Evaluate> setList() {
        List<Evaluate> dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Evaluate evaluate = new Evaluate();
            evaluate.setTitle_ev("海绵宝宝强迫症");
            evaluate.setData_ev("2017-5-25");
            evaluate.setTime_ev("11:00am");
            evaluate.setComment("鸡蛋很新鲜,跟小时候在农村时吃的一样，物有所值...");
            dataList.add(evaluate);
        }
        return dataList;

    }
    @Override
    public void onRefresh() {
        Log.e("wxl", "onRefresh");
        setRefresh();
        getData();
    }

    @Override
    public void onLoadMore() {
        Log.e("wxl", "onLoadMore");
        mCount = mCount + 1;
        getData();
    }

    private void setRefresh() {
        mRecyclerViewAdapter.clearData();
        mCount = 1;
    }
}
